package com.xci.platform.web.aspect;

import com.xci.platform.auth.core.AuthHelper;
import com.xci.platform.auth.core.AuthUser;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.Serializable;

/**
 * 自动注入当前操作用户字段值
 */
@Slf4j
@Aspect
@Order(1)
@Component
public class OperateUserAspect {

    @Resource
    protected AuthHelper authHelper;

    @Before("@annotation(com.xci.platform.annotation.OperateUser)")
    public void doBefore(JoinPoint point) {
        // ServletRequestAttributes servlet = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        // if (servlet == null) {
        //     return;
        // }
        // AuthUser user = null;
        // HttpServletRequest request = servlet.getRequest();
        // if (Util.isApp(request)) {
        // } else {
        //     user = request.getSession().getAttribute(WebConst.UserSessionKey);
        // }
        AuthUser user = authHelper.currentUser();
        Object[] args = point.getArgs();// 获得切入方法参数
        for (Object bean : args) {
            if (bean instanceof Serializable) {
                authHelper.setOperateUser(bean, user);
            }
        }
    }
}